package gui_layer;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;

import model_layer.StaffMember;
import ctr_layer.StaffMemberCtr;
import db_layer.CustomerDB;
import db_layer.StaffMemberDB;

@SuppressWarnings("serial")
public class LoginWindow extends JFrame
{

	private JPanel contentPane;
	private JTextField username_tf;
	private JPasswordField passwordField;
	private StaffMemberCtr sm_ctr;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args)
	{
		EventQueue.invokeLater(new Runnable()
		{
			public void run()
			{
				try
				{
					CustomerDB cust_db = new CustomerDB();
					StaffMemberDB sm_db = new StaffMemberDB();
					sm_db.check_if_any_account_exist();
					cust_db.check_for_regular();
					
					UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
					LoginWindow frame = new LoginWindow();
					frame.setVisible(true);
				}
				catch (Exception e)
				{
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public LoginWindow()
	{
		sm_ctr = new StaffMemberCtr();
		setFont(new Font("Helvetica Neue", Font.BOLD, 14));
		setResizable(false);
		setTitle("Sign in");
		try
		{
			setIconImage(Toolkit.getDefaultToolkit().getImage(LoginWindow.class.getResource("/img/icon.png")));
		}
		catch (Exception e)
		{
		}

		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 278, 209);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setBackground(Color.getHSBColor(312, 0.11f, 0.17f));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		make_lbls();
		make_tfs();
		make_btns();
		
	}
	
	private void make_tfs()
	{
		username_tf = new JTextField();
		username_tf.setBounds(127, 40, 123, 23);
		username_tf.setBorder(new RoundedCornerBorder());
		username_tf.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		username_tf.addKeyListener(new KeyAdapter()
		{
			public void keyPressed(KeyEvent e)
			{
				if(e.getKeyCode() == KeyEvent.VK_ENTER)
				{
					passwordField.requestFocus();
				}
			}
		});
		contentPane.add(username_tf);
		username_tf.setColumns(10);
		
		passwordField = new JPasswordField();
		passwordField.setBorder(new RoundedCornerBorder());
		passwordField.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		passwordField.addKeyListener(new KeyAdapter()
		{
			public void keyPressed(KeyEvent e)
			{
				if(e.getKeyCode() == KeyEvent.VK_ENTER)
				{
					sign_in();
				}
			}
		});
		passwordField.setBounds(127, 74, 123, 23);
		contentPane.add(passwordField);
	}
	
	private void make_lbls()
	{
		JLabel lblUsername = new JLabel("Username");
		lblUsername.setFont(new Font("Helvetica Neue", Font.BOLD, 14));
		lblUsername.setForeground(Color.WHITE);
		lblUsername.setBounds(28, 43, 89, 14);
		contentPane.add(lblUsername);
		
		JLabel lblPassword = new JLabel("Password");
		lblPassword.setFont(new Font("Helvetica Neue", Font.BOLD, 14));
		lblPassword.setForeground(Color.WHITE);
		lblPassword.setBounds(28, 77, 77, 14);
		contentPane.add(lblPassword);
	}
	
	private void make_btns()
	{
		float[] values = Color.RGBtoHSB(255, 104, 0, null);
		JButton btnOk = new JButton("OK");
		btnOk.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		btnOk.setForeground(Color.WHITE);
		btnOk.setBackground(Color.getHSBColor(values[0], values[1], values[2]));
		btnOk.setContentAreaFilled(false);
		btnOk.setOpaque(true);
		btnOk.setBorder(new RoundedCornerBorder());
		btnOk.setBounds(10, 147, 89, 23);
		btnOk.addActionListener(new ActionListener()
		{
			
			@Override
			public void actionPerformed(ActionEvent arg0)
			{
				sign_in();
				
			}
		});
		contentPane.add(btnOk);
		
		JButton btnExit = new JButton("Exit");
		btnExit.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		btnExit.setForeground(Color.WHITE);
		btnExit.setBackground(Color.getHSBColor(values[0], values[1], values[2]));
		btnExit.setContentAreaFilled(false);
		btnExit.setOpaque(true);
		btnExit.setBorder(new RoundedCornerBorder());
		btnExit.setBounds(173, 147, 89, 23);
		btnExit.addActionListener(new ActionListener()
		{
			
			@Override
			public void actionPerformed(ActionEvent arg0)
			{
				dispose();
				
			}
		});
		contentPane.add(btnExit);
	}
	
	private void sign_in()
	{
		String username = username_tf.getText();
		String password = String.valueOf(passwordField.getPassword());
		if(username.length() != 0 && password.length() != 0)
		{
			StaffMember current_account = sm_ctr.log_in_staff_member(username, password);
			if(current_account != null)
			{
				CurrentAccount.get_instance().set_current_account(current_account);
				MainWindow mw = new MainWindow();
				mw.setVisible(true);
				dispose();
			}
			else
			{
				JOptionPane.showMessageDialog(this, "Incorrect username or password.", "Cannot sign in", JOptionPane.ERROR_MESSAGE);
			}
		}
		else
		{
			JOptionPane.showMessageDialog(this, "You must input username and password.", "Cannot sign in", JOptionPane.ERROR_MESSAGE);
		}
	}
}
